import mathUtil from "./mathUtil";
const getSystemInfo = ()=>{
    return new Promise((resolve, reject) => {
        uni.getSystemInfo({
            success: function (res) {
                resolve(res);
            },
            fail(res) {
                reject(res);
            }
        });
    });
}
const getContext = ()=>{
    let pages = getCurrentPages();
    return pages[pages.length-1];
}

const getClientTime = ()=>{
    return new Promise((resolve,reject) => {
        FuncAPP(()=>{
            try{
                let key = "max-fire-client-id";
                let id = uni.getStorageSync(key);
                if(id){
                    resolve(id);
                } else {
                    let pinf = plus.push.getClientInfo();
                    let cid = pinf && pinf.clientid || ''; //客户端标识
                    if(!cid){
                        plus.push.getClientInfoAsync((info) => {
                            cid = info?.clientid;
                            if(cid){
                                uni.setStorage({
                                    key:key,
                                    data:cid
                                })
                                resolve(cid+"");
                            } else {
                                reject("");
                            }
                        }, err => {
                            reject("");
                        });
                    } else {
                        uni.setStorage({
                            key:key,
                            data:cid
                        })
                        resolve(cid);
                    }
                }
            }catch (e){
                reject(e);
            }
        });
        FuncNotAPP(()=>{
            resolve("");
        });
    });
}

const getClientInfo = async () => {
    try{
        let res = await getClientTime();
        return res;
    } catch (e){
        let res = await new Promise((resolve,reject) => {
            setTimeout(async ()=>{
                getClientTime().then(res=>{
                    resolve(res);
                }).catch(e=>{
                    resolve("");
                });
            },500);
        });

        return res;
    }
}

const navigateBack = (time=2000,func,index=1)=>{
    setTimeout(()=>{
        if(func){
            func();
        } else {
            uni.navigateBack({
                delta:index
            });
        }

    },time)
}

const deadlineTimeStr = (item)=>{
    let res = {};
    // 任务倒计时逻辑：倒计时 = 当前时间 - 预计完成时间，倒计时时长 < 预计任务时长 * 20%时红色紧急提醒
    if(item && item.expEndTime && item.status == 40){
        let dTime = mathUtil.accSub(new Date(item.expEndTime.replace(/-/g,"/")).getTime(),new Date().getTime());
        if(dTime<0){
            dTime = 0;
        } else {
            dTime = mathUtil.accDiv(dTime,1000);
            res.expDuration = mathUtil.accMul(mathUtil.accMul(item.expDuration || 0,60),0.2);
        }
        res.dTime = dTime;
        return res;
    } else {
        return res;
    }
}

const FuncAPP = (func)=>{
    // #ifdef APP-PLUS
    return func();
    // #endif
}
const FuncH5 = (func)=>{
    // #ifdef H5
    func();
    // #endif
}
const FuncNotAPP = (func)=>{
    // #ifndef APP-PLUS
    func();
    // #endif
}
const noRTime = (status)=>{
    if(status == null || status == ''){
        return false
    }
    return ![50,60,37].includes(status)
}

const showLoading = (title)=>{
    uni.showLoading({mask:true,title})
}

const showJobQrCode = (id,context) =>{
    // let context = getContext();
    const code = `job-zr:${id};${context.$store.state.pin}`
    console.log(code)
    context.showModal = true
    context.code = code
    context.$refs.qrCode.showQrcode()
}

export default {
    showLoading,getContext,getSystemInfo,getClientInfo,navigateBack,FuncAPP,FuncNotAPP,FuncH5,deadlineTimeStr,noRTime,showJobQrCode,
    accAdd:mathUtil.accAdd,//加法
    accSub:mathUtil.accSub,//减法
    accDiv:mathUtil.accDiv,//除法
    accMul:mathUtil.accMul,//乘法
}
